package com.limbo.showapp.base;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.limbo.showapp.util.ToastUtils;


/**
 * Created by Administrator on 2017/5/22.
 */

public abstract class BaseFragment<V extends IBaseView, P extends BasePresenter> extends Fragment {
    protected Activity mActivity;
    protected P mPresenter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mActivity = getActivity();
        return inflater.inflate(getLayoutId(), container, false);
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        try {
            mPresenter = getPresenter();
            if (mPresenter != null)
                mPresenter.attachView((V) this);

            initView(view);
            initData();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ToastUtils.getInstanc(getContext()).destory();
        if (mPresenter != null)
            mPresenter.detachView();
    }

    //抽象方法 获取Presenter对象
    protected abstract P getPresenter();

    public abstract int getLayoutId();

    public abstract void initView(View view);

    public abstract void initData();
}

